<?
	require_once "../../_func/__Global.php";
	require_once "../../_func/__Local.php";
	require_once "../../_func/__MysqlFunc.php";
	require_once "../../_func/_MngFunc.php";
	require_once "../../_func/_Validation.php";
	require_once "../../_func/_CodeFunc.php";
	
	if ( ValidCheck_isSelfRequest($_SERVER['HTTP_REFERER']) ) {
		if ( $mode == 'r' ) {
			//get parameter
			$jbnh_seq 			= $param->getString('jbnh_seq');
			$farm_name 			= $param->getString('farm_name');
			$farm_address 		= $param->getString('farm_address');
			$farm_tel 			= $param->getString('farm_tel');
			$farm_itemcode1 	= $param->getString('farm_itemcode1');
			$farm_itemcode2 	= $param->getString('farm_itemcode2');
			$farm_regnumber 	= $param->getString('farm_regnumber');
			$farm_certnumber 	= $param->getString('farm_certnumber');
			$farm_gapnumber 	= $param->getString('farm_gapnumber');
			$farm_tracenumber	= $param->getString('farm_tracenumber');
			$farm_certdate 		= $param->getString('farm_certdate');

			
			//공백허용하지 않는것을 먼저 등록하여 유효성 검사
			$insert_arr = array(
				'jbnh_seq'			=> $jbnh_seq,
				'farm_name'			=> $farm_name,
				'farm_address'		=> $farm_address,
				'farm_tel'			=> $farm_tel,
				'farm_itemcode1'	=> $farm_itemcode1,
				'farm_regnumber'	=> $farm_regnumber,
				'farm_certnumber'	=> $farm_certnumber,
				'farm_gapnumber'	=> $farm_gapnumber,
				'farm_tracenumber'	=> $farm_tracenumber,
				'farm_certdate'		=> $farm_certdate,
			);
			
			$isValid = true;
			foreach( $insert_arr as $key => $val )
			{
				if (!ValidCheck_isNotEmpty($val)) {
					errMsg($VALIDATE_ERROR_MESSAGE);
					$isValid = false;
				}
			}
			
			if ( $isValid )
			{
				$insert_arr['farm_memo'] = $farm_memo;

				//농가등록후 번호 취득
				$farm_seq	= FarmInsert( $insert_arr );
				
				
				//품종 등록
				if( $farm_seq > 0 )
				{
					if ( is_array( $farm_itemcode2 ) ) {
						foreach( $farm_itemcode2 as $key => $val )
						{
							$sub_arr = array(
								'farm_seq'			=> $farm_seq,
								'farmitem_item2'	=> $val,
							);		
							FarmItemInsert($sub_arr);
							unset($sub_arr);
						}
					}
					cacheControl(0);
					ob_clean();
					jsMsg("등록되었습니다.",'/mng/farm.html',true);
				}
			}
		}
		else {
			errMsg("비정상적인 접근입니다.");
		}
	} else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>:: 농협전북지부 ERP시스템 ::</title>
<link href="/css/layout.css" type="text/css" rel="stylesheet" />
<script src="/js/mng.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
	
		//======================================>>> ready for calendar
		
		if ( isNeedCAL ) {
			$(function(){
				$('#farm_certdate').datepicker({
					inline: true,
					showOn: 'button',
					buttonImage:"/img/ico_cal.jpg",
					buttonImageOnly: true
				});
				//hover states on the static widgets
				$('#dialog_link, ul#icons li').hover(
					function() { $(this).addClass('ui-state-hover'); },
					function() { $(this).removeClass('ui-state-hover'); }
				);
			});
		}
		//======================================>>> ready for calendar


		jQuery.validator.setDefaults({
			onkeyup:false,
			onclick:false,
			onfocusout:false,
			showErrors:function(errorMap, errorList){
				$(errorList[0].element).focus();
				alert(errorList[0].message);
			}
		});
		$("#frm").validate({
			rules: {
				jbnh_seq: 			{required: true},
				farm_name:			{required: true},
				farm_address:		{required: true},
				farm_tel:			{required: true , telNum:true},
				farm_itemcode1:		{required: true},
				"farm_itemcode2[]":	{required: true},
				farm_regnumber:		{required: true , jbnhCertNo:true},
				farm_certnumber:	{required: true , jbnhCertNo:true},
				farm_gapnumber:		{required: true , jbnhCertNo:true},
				farm_tracenumber:	{required: true , jbnhCertNo:true},
				farm_certdate:		{required: true}
			},
			messages: {
				jbnh_seq: 			{required: "소속농협을 선택하세요"},
				farm_name:			{required: "생산자를 입력하세요"},
				farm_address:		{required: "주소를 입력하세요"},
				farm_tel:			{required: "연락처를 입력하세요"},
				farm_itemcode1:		{required: "품목을 체크하세요"},
				"farm_itemcode2[]":	{required: "품종을 체크하세요"},
				farm_regnumber:		{required: "출하자신고번호를 입력하세요"},
				farm_certnumber:	{required: "품질인증번호를 입력하세요"},
				farm_gapnumber:		{required: "GAP인증번호를 입력하세요"},
				farm_tracenumber: 	{required: "이력추적관리번호을 입력하세요"},
				farm_certdate:		{required: "인증만료기간을 입력하세요"}
			},
			success: function(label) {
				$('#sbmtBtn').remove();
			}
		});
	});
</script>
</head>
<body>
<div id="wrap">
	<!-- header start -->
	<? include("../_layout/head.inc"); ?>
	<!-- header end -->
	
<!-- container start -->
	<div id="container">
	
	<!-- sub menu start -->
	<? include("../_layout/left.inc"); ?>
	<!-- sub menu end -->	
	
		<div class="code_contents">
			<div class="subtit">
				<p class="subtt21"><span>코드관리 > 농가 관리</span></p>
			</div>
			<p class="tableTitle"><span class="b"></span>농가등록</p>
			<form name=frm id=frm method="POST" action="<?echo $PHP_SELF?>">
			<table cellpadding="0" cellspacing="1" border="0" class="erpTable rowLeft">
           		<colgroup>
                  <col width="200px" />
                  <col width="*" />
                  </colgroup>
                <tr>
                    <th>소속농협 <span class="basic">*</span></th>
                    <td>
						<?echo GetJBNHOptionList(null) ?>
                    </td>
				</tr>
				<tr>
                    <th>생산자 <span class="basic">*</span></th>
                    <td><input type="text" id="farm_name" name="farm_name" class="boxinput w100" /></p>
                    </td>
				</tr>
				<tr>
                    <th>주소 <span class="basic">*</span></th>
                    <td><input type="text" id="farm_address" name="farm_address" class="boxinput w97p" /></p>
                    </td>
				</tr>
				<tr>
                    <th>연락처 <span class="basic">*</span></th>
                    <td><input type="text" id="farm_tel" name="farm_tel" class="boxinput w100" />
                    	<p class="comment">*생산자연락처는 "-"와 함께 입력해주세요.</p>
                    </td>
				</tr>
				<tr>
                    <th>품목 <span class="basic">*</span></th>
                    <td>
                    	<select name="farm_itemcode1" id="farm_itemcode1" onchange="fncGetFarmItem2('item', 2, this.value, $('#div_item2')  );">
                        	<option value="">품목명</option>
							<?
								$codedata = & Code_GetSelect("A", 1, 0);
								Code_PrintOption($codedata,1);
							?>
                        </select>
                    </td>
				</tr>
				<tr>
                    <th>품종 <span class="basic">*</span></th>
                    <td><div  id="div_item2" name="div_item2"></div>
                    </td>
				</tr>
                
                <tr>
                    <th>출하자 신고번호 <span class="basic">*</span></th>
                    <td><input type="text" id="farm_regnumber" name="farm_regnumber" class="boxinput w100" />
						<p class="comment">*출하자 신고번호는 "-"와 함께 입력해주세요.</p>
					</td>
				</tr>
				<tr>
                    <th>품질인증번호 <span class="basic">*</span></th>
                    <td><input type="text" id="farm_certnumber" name="farm_certnumber" class="boxinput w100" />
						<p class="comment">*품질인증번호는 "-"와 함께 입력해주세요.</p>
					</td>
				</tr>
				<tr>
                    <th>GAP인증번호 <span class="basic">*</span></th>
                    <td><input type="text" id="farm_gapnumber" name="farm_gapnumber" class="boxinput w100" />
						<p class="comment">*GAP인증번호는 "-"와 함께 입력해주세요.</p>
					</td>
				</tr>
				<tr>
                    <th>이력추적관리번호 <span class="basic">*</span></th>
                    <td><input type="text" id="farm_tracenumber" name="farm_tracenumber" class="boxinput w100" />
						<p class="comment">*이력추적관리번호는 "-"와 함께 입력해주세요.</p>
					</td>
				</tr>
				<tr>
                    <th>인증만료기간 <span class="basic">*</span></th>
                    <td><input type="text" id="farm_certdate" name="farm_certdate" title="만료일입력/달력을클릭해주세요." readonly class="boxinput w100" style="margin-right:2px;vertical-align:top;" />
					</td>
				</tr>
                
				<tr>
                    <th>비고사항</th>
                    <td><textarea id="farm_memo" name="farm_memo" class="note"></textarea>
                    </td>
				</tr>
            </table>
            <div class="BtnList">
            	<ul>
                	<li><input type="submit" id="sbmtBtn" name="sbmtBtn" class="btn_submit" value=""/></li>
					<li><input type="button" class="btn_cancel" onClick="MNG.fncRtnfunc('rst','farm');" title="취소"/></li>
					<input type="hidden" name="mode" id="mode" value="r"/>
                </ul>
            </div>
			</form>
		</div>
	</div>
	<!-- footer start -->
	<? include("../_layout/footer.inc"); ?>
	<!-- footer end -->
</div>
</body>
</html>
<?
	}
?>